package com.ug_project.views

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.provider.Settings
import android.support.v4.content.ContextCompat
import android.telephony.TelephonyManager
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import com.pawegio.kandroid.telephonyManager
import com.pawegio.kandroid.wifiManager
import com.ug_project.adapters.BaseAdapter
import com.ug_project.adapters.createBaseAdapter
import com.ug_project.objects.Tuple2.Tuple
import java.util.ArrayList

/**
 * Created by yeganaaa on 2/17/2017.
 */

fun Context.showToast(text: String) : Unit = Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
fun Context.showLongToast(text: String) : Unit = Toast.makeText(this, text, Toast.LENGTH_LONG).show()
val Context.layoutInflater: LayoutInflater
get() = LayoutInflater.from(this)

val Context.inputManager: InputMethodManager
    get() = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager


// كونۇپكا تاختىسى ئۇچۇق بولسا يۇشۇرىدۇ يۇشۇرۇن ھالەتتە بولسا كۆرسىتىدۇ

fun Context.toggleKeyboard() {
    inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
}


//تېلفۇن قىلىش كۆزنىكىگە ئۆتىدۇ
var Context.call: String
    get() = ""
    set(value) {
        val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + value))
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        startActivity(intent)
    }

fun <T> Context.createSingleAdapter(data: ArrayList<T>, where: (position_and_data: Tuple<Int, T>) -> String, action: ((position_and_data: Tuple<Int, T>) -> Unit)? = null): BaseAdapter<T> {
    return createBaseAdapter(this, R.layout.item_single, data) {
        GetViewIData { position, rootView, parent, data ->
            val content = rootView.findTextView(R.id.Name)
            content.text = where(Tuple(position, data))
            rootView.click {
                action?.invoke(Tuple(position, data))
            }
        }
    }
}

/**
 * نۆۋەتتىكى ئەپنىڭ باشقۇرۇش رايۇنىنى قوزغىتىش
 */
fun Context.startThisAppSettings() {
    var intent = Intent()
    intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
    var uri = Uri.fromParts("package", packageName, null)
    intent.data = uri
    startActivity(intent)
}

fun Context.checkPermiss(permission: String): Boolean {
    return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this, permission)
}

val Context.wifiMacId: String
    get() {
        return wifiManager?.connectionInfo?.macAddress ?: "null"
    }

fun Context.startAct(cls: Class<*>) {
    this.startActivity(android.content.Intent(this, cls))
}

fun Context.startService(cls: Class<*>) {
    this.startService(android.content.Intent(this, cls))
}

val Context.deviceId: String
    get() {
           return telephonyManager?.deviceId ?: android.os.Build.SERIAL ?: Settings.System.getString(contentResolver, Settings.System.ANDROID_ID) ?: telephonyManager?.simSerialNumber ?: "unknown"
    }
